package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.po.Linking;
import com.example.demo.service.LinkService;

@Controller
@RequestMapping("/admin")
public class AdminLinkingController {
	
	@Autowired	
	private LinkService linkingService;
	
	@RequestMapping(value="/adminLinking.action",method=RequestMethod.GET)
	public String indext(Model model){
		
		List<Linking> linkingList = linkingService.selectAll();
		
		model.addAttribute("linkingList", linkingList); 
		
		return "admin/linking/linkingList";
		
	}
	
	@RequestMapping(value="/toEditLinking.action",method=RequestMethod.GET)
	public String toEdit(Model model,Integer id){
		
		if(id!=null){
			
			Linking linking=linkingService.selectById(id);
			
			model.addAttribute("linking", linking);
		}
		
		return "admin/linking/edit";
		
	}
	
	@RequestMapping(value="/editLinking.action",method=RequestMethod.POST)
	public String edit(Linking linking){
		
		if(linking.getId()!=null){
			linkingService.update(linking);
		}else{
			linkingService.insert(linking);
		}
		
		return "redirect:adminLinking.action";		
	}
	
	@RequestMapping(value="/deleteLinking.action",method=RequestMethod.GET)
	public String delete(Integer id){
		
		linkingService.delete(id);
		return "redirect:adminLinking.action";
		
	}

}
